package org.fuxia.im.core.service.impl;

import org.fuxia.im.core.entries.ECPacket;
import org.fuxia.im.core.entries.ECResponse;
import org.fuxia.im.core.entries.ECSession;
import org.fuxia.im.core.iconst.ServerConst;
import org.fuxia.im.core.server.ECSessionMananger;
import org.fuxia.im.core.service.ECService;
import org.fuxia.im.core.socket.impl.ECNioServer;

/**
 * 抽象Service，提供了向客户端回写消息的方法
 *
 */
public abstract class AbstractService implements ECService
{
	protected void writeReponse(ECPacket packet , ECResponse resp) 
	{
		ECSession session = null;
		if(packet.getFrom()!=null) {
			session = ECSessionMananger.getInstance().getUserSession(packet.getFrom()) ;
		}
		if(session==null) {
			session = ECSessionMananger.getInstance().getSession(packet.getParameter(ServerConst.SESSION_ID.name()));
		}
		
		if(session!=null) {
			try {
				ECNioServer.writeResponse(resp, session.getChannel());
			} catch (Exception e) {
				//no ops
			}
		}
	}


}
